package dao

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type HttpRule struct {
	Id             int    `json:"id" gorm:"primary_key" description:"自增主键"`
	ServiceId      int    `json:"service_id" gorm:"column:service_id" description:"服务Id"`
	RuleType       int    `json:"rule_type" gorm:"column:rule_type" description:"匹配类型 domain=域名 url_prefix=url前缀"`
	Rule           string `json:"rule" gorm:"column:rule" description:"RuleType=domain表示域名 RuleType=url_prefix表示url前缀"`
	NeedHttps      int    `json:"need_https" gorm:"column:need_https" description:"是否支持https 1支持"`
	NeedWebsocket  int    `json:"need_websocket" gorm:"column:need_websocket" description:"是否启用websocket 1启用"`
	NeedStripUri   int    `json:"need_strip_uri" gorm:"column:need_strip_uri" description:"启用strip_uri 1启用"`
	UrlRewrite     string `json:"url_rewrite" gorm:"column:url_rewrite" description:"url重写 每行一个"`
	HeaderTransfor string `json:"header_transfor" gorm:"column:header_transfor" description:"header转换支持增加(add)、删除(del)、修改(edit) 格式：add headname headvalue"`
}

func (h *HttpRule) TableName() string {
	return "gateway_service_http_rule"
}

func (h *HttpRule) Find(c *gin.Context, tx *gorm.DB, search *HttpRule) (*HttpRule, error) {
	httpRule := &HttpRule{}
	// 根据结构体查询
	err := tx.WithContext(c).Where(search).First(httpRule).Error
	return httpRule, err
}

func (h *HttpRule) Save(c *gin.Context, tx *gorm.DB) error {
	err := tx.WithContext(c).Save(h).Error
	return err
}
